const request = require('request')
const http = require('https');
const md5 = require("md5")
const ws = require("ws")
const pwd =  md5("886521qq")
const t = `${Date.now()}`
const logins = {
    'Action': "login",
    'Message': "72"
}
const getsubscribe = {
    'Action': "getsubscribe",
    'Message': "",
    'data': "null"
}
const limitgame = {
    'Action': "limitgame",
    'Message': "",
    'data': "null"
}
const linkserver = {
    'Action': "linkserver",
    'Message': "",
    'data': "null"
}
const ping={
    'Action': "ping",
    'Message': "pc",
  
}
const actList = [
    "13100611280",'16583072828',"19239628153","19209213794","19231143280","19231143349","19239036140","19271034751","19239616302","19271036648",
    "19231143348","19211517030",'15189807232','16215296360','19211515308','17112524616','16792735259','16571698947','17112522673','16590450610',
    '19274653093','19239628459','15117730565','19232513540','19271036427',"19271034860","19213460014","19231143248","19211554273","19232589542",
    "19231143120","19231143698","19211548925","19271034967","19271172697","19211515102","19239006724","19239618775","13985356126","19274629825"
]

function login (username){
    const postData = JSON.stringify({
        username: username,
        password: pwd,
        T: t,
        Sign: md5( username+"wo1wan" + pwd + t)
    })
    const options = {
        hostname: 'www.wo1wan.com', // API服务器地址
        path:"/nextgame/open/igwlogin",
        method: 'POST', // 请求方法为POST
        headers: {
            'Content-Type': 'application/json;charset=UTF-8', // 指定请求体格式为JSON
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
        },
    };
    const req = http.request(options, (res) => {
        let data = '';
        
        res.on('data', (chunk) => {
            data += chunk;
        });
      
        res.on('end', () => {
            const str = `${res.headers['set-cookie']}`.match(/igwflyfree=igwsession(\S*);/)[1]
            const ck = `${res.headers['set-cookie']}` +";player=pcsession" + str + ";igwlllog=MTcxLjM1LjExNy4yNTQ5YTI5N2Q5Y2M0ZWQ5MzBkOWVjMmY2NzFkNTkyZmMyOQ%3D%3D"
            const option = {
                hostname: 'play.wo1wan.com', // API服务器地址
                path:"/nextgame/igwuser/userinfo",
                method: 'GET', // 请求方法为POST
                headers: {
                    'Content-Type': 'application/json;charset=UTF-8', // 指定请求体格式为JSON
                    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
                    "Cookie": ck
                },
            }
            const reqs = http.request(option, (res1) => {
                let data = '';
                res1.on('data', (chunk) => {
                    data += chunk;
                });
                res1.on("end", () => {
                    const info = JSON.parse(data)
                    connectWss(ck,info.info.NickName)
                })
            })
            reqs.end()
        });
    });
    req.write(postData)
    req.end()
}


/**  function connectWss(ck,nickname,index) {
    function heartbeat(ws) {
        clearTimeout(this.pingTimeout);
      
        // Use `WebSocket#terminate()`, which immediately destroys the connection,
        // instead of `WebSocket#close()`, which waits for the close timer.
        // Delay should be equal to the interval at which your server
        // sends out pings plus a conservative assumption of the latency.
        this.pingTimeout = setTimeout(() => {
          ws.send(JSON.stringify(ping))
        }, 30000 + 1000);
      }
    const wss = new ws("wss://play.wo1wan.com/nextgame/igwserver",{
        headers: {
            'Cookie': ck,
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
        }
    })
    var timer = setInterval(() => {
        wss.send(JSON.stringify(ping))
    },50*1000)
    wss.onopen = function(){
        wss.send(JSON.stringify(logins))
        wss.send(JSON.stringify(getsubscribe))
        wss.send(JSON.stringify(linkserver))
        wss.send(JSON.stringify(limitgame))
        heartbeat(wss)
    }
    wss.on('ping', heartbeat);
    wss.on('close', function clear() {
        clearInterval(timer);
      });
    wss.onmessage = function(e){
        if(e.data.indexOf("getxp")>0){
            const data = JSON.parse(e.data)
            const lv = data.Data.Level
            const xp = data.Data.NowXP
            const nextXp = data.Data.NextLevel
            console.log(`当前用户名：${nickname}，当前等级：${lv}，当前经验：${xp}，升级经验：${nextXp}`)
        }
    }
    
}
*/

function connectWss(ck) {
    function heartbeat(ws) {
        clearTimeout(this.pingTimeout);
      
        // Use `WebSocket#terminate()`, which immediately destroys the connection,
        // instead of `WebSocket#close()`, which waits for the close timer.
        // Delay should be equal to the interval at which your server
        // sends out pings plus a conservative assumption of the latency.
        this.pingTimeout = setTimeout(() => {
          ws.send(JSON.stringify(ping))
        }, 30000 + 1000);
      }
    const wss = new ws("wss://play.wo1wan.com/nextgame/igwserver",{
        headers: {
            'Cookie': ck,
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
        }
    })
    var timer = setInterval(() => {
        wss.send(JSON.stringify(ping))
    },50*1000)
    wss.onopen = function(){
        wss.send(JSON.stringify(logins))
        wss.send(JSON.stringify(getsubscribe))
        wss.send(JSON.stringify(linkserver))
        wss.send(JSON.stringify(limitgame))
        heartbeat(wss)
        console.log("success")
    }
    wss.on('ping', heartbeat);
    wss.on('close', function clear() {
        clearInterval(timer);
      });
}
connectWss("where=sz; UM_distinctid=18f36e8e37f769-00978f157e45a4-26001b51-7e9000-18f36e8e380ca0; CNZZDATA1278009588=1835609458-1714613183-%7C1714613183; igwflyfree=igwsession78c2bc8e05a920d8ee784c159c22433abf61d3186a0da80ce6c498b7e3782df1; igwlllog=MTcxLjM1LjExNi41MDc4YzJiYzhlMDVhOTIwZDhlZTc4NGMxNTljMjI0MzNh; pmode=pc; player=pcsession78c2bc8e05a920d8ee784c159c22433abf61d3186a0da80ce6c498b7e3782df1")